import http, {token} from './http.js'
import {
	USER_LOCATION_STORE_URL,
	ALL_STORE_LOCATION_URL,
	USER_LOCATION_URL,
	USER_NEARBY_STORE_URL,
	USER_LOCATION_TIPS_URL,
	USER_RECEIVE_ADDRESS_URL
} from './url.js'

// 获取用户附近门店
const getUserNearbyStores = (latitude, longitude) => {
	return http({
		method: 'post',
		url: USER_NEARBY_STORE_URL,
		data: {
			"appId": 1005,
			"apiVersion": "7.0",
			"userToken": token,
			"storeNo": "",
			"data": {
				"latitude": latitude, // 21.844067,
				"longitude": longitude, //111.969229,
			}
		}
	}).then(res => {
		return res
	}).catch(err => {
		console.log('getUserNearbyStores-err---', err)
	})
}

const getUserLocation = (latitude, longitude) => {
	return new Promise((resolve, reject) => {
		uni.request({
			method: 'GET',
			// #ifdef MP-WEIXIN || APP-PLUS
			url: 'https://restapi.amap.com/v3/geocode/regeo',
			// #endif
			// #ifdef H5
			url: '/v3/geocode/regeo',
			// #endif
			data: {
				key: '0b314319a91963ac53054b9bf7d48d5f',
				extensions: 'all',
				s: 'rsx',
				platform: 'WXJS',
				appname: '0b314319a91963ac53054b9bf7d48d5f',
				sdkversion: '1.2.0',
				logversion: '2.0',
				location: `${longitude},${latitude}`
			},
			success(res) {
				resolve(res)
			},
			fail(err) {
				reject(err)
			}
		})
	})
}

// 获取关键词所有地址提示
const getUserLocationTips = (keywords) => {
	return new Promise((resolve, reject) => {
		uni.request({
			method: 'GET',
			// #ifdef MP-WEIXIN || APP-PLUS
			url: 'https://restapi.amap.com/v3/assistant/inputtips',
			// #endif
			// #ifdef H5
			url: '/v3/assistant/inputtips',
			// #endif
			data: {
				key: '0b314319a91963ac53054b9bf7d48d5f',
				s: 'rsx',
				platform: 'WXJS',
				appname: '0b314319a91963ac53054b9bf7d48d5f',
				sdkversion: '1.2.0',
				logversion: '2.0',
				keywords
			},
			success(res) {
				resolve(res)
			},
			fail(err) {
				reject(err)
			}
		})
	})
}

// 根据 ip 获取用户坐标（经纬度）
const getUserCoordinate = () => {
	return new Promise((resolve, reject) => {
		uni.request({
			method: 'GET',
			// #ifdef MP-WEIXIN || APP-PLUS
			url: 'https://apis.map.qq.com/ws/location/v1/ip',
			// #endif
			// #ifdef H5
			url: '/ws/location/v1/ip',
			// #endif
			data: {
				key: 'RPEBZ-J2ZWW-2CIRC-OO3Q2-HH7X2-7GBEJ',
				sig: '7817334a06d1ddb3bac9137847e65b6b'
			},
			success(res) {
				resolve(res)
			},
			fail(err) {
				reject(err)
			}
		})
	})
}

const getUserLocationStore = (latitude, longitude) => {
	return http({
		method: 'post',
		url: USER_LOCATION_STORE_URL,
		data: {
			"appId": 1005,
			"apiVersion": "5.0",
			"userToken": token,
			"data": {
				"latitude": latitude, // 21.844067,
				"longitude": longitude, //111.969229,
				"storeNo": ""
			}
		}
	}).then(res => {
		return res
	}).catch(err => {
		console.log('getUserLocationStore-err---', err)
	})
}

const getAllStoreLocation = () => {
	return http({
		method: 'post',
		url: ALL_STORE_LOCATION_URL,
		data: {
			"appId": 1005,
			"apiVersion": "5.0",
			"isShowLoading": true,
			"userToken": token,
			"data": {
				"userId": 3644328
			}
		}
	}).then(res => {
		return res
	}).catch(err => {
		console.log('getAllStoreLocation-err---', err)
	})
}

const getUserReceiveAddress = (storeNo) => {
	return http({
		method: 'post',
		url: USER_RECEIVE_ADDRESS_URL,
		data: {
			"appId": 1005,
			"apiVersion": "7.0",
			"isShowLoading": true,
			"userToken": token,
			"userId": 3644328,
			"storeNo": storeNo,
			"data": {}
		}
	}).then(res => {
		return res
	}).catch(err => {
		console.log('getUserReceiveAddress-err---', err)
	})
}

export {
	getUserNearbyStores, 
	getUserLocation, 
	getUserLocationStore,
	getAllStoreLocation,
	getUserLocationTips,
	getUserCoordinate,
	getUserReceiveAddress
}